<template>
	<view>
		<image src="../../static/logo-2.png" mode="widthFix" class="logo"></image>
		<view class="register-container">
			<input type="text" placeholder="请输入你的邀请码" maxlength="6" v-model="registerCode" class="register-code" />
			<view class="register-desc">管理员创建员工账号之后,可以从你的邮箱获取注册邀请码</view>
			<button class="register-btn" open-type="getUserInfo" @tap="register">注册用户</button>

		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				registerCode: ""
			}
		},
		methods: {
			register() {
				let that = this
				if (that.registerCode == null || that.registerCode.length == 0) {
					uni.showToast({
						icon: "none",
						title: "邀请码不能为空"
					})
					return
				} else if (/^[0-9]{6}$/.test(that.registerCode) == false) {
					uni.showToast({
						icon: "none",
						title: "邀请码必须是6位数字"
					})
					return
				}
				uni.login({
					provider: "weixin",
					success(res) {
						/* 回调函数中的this指的是当前的回调函数,不是vue对象*/
						// 获取临时授权码
						console.log(res.code)
						let code = res.code
						// 获取用户基本信息
						uni.getUserInfo({
							provider: "weixin,",
							success(resp) {
								let nickname = resp.userInfo.nickName
								let avatarUrl = resp.userInfo.avatarUrl
								let data = {
									code:code,
									nickname:nickname,
									photo:avatarUrl,
									registerCode:that.registerCode
								}
								that.ajax(that.url.register,'POST',data,resp=>{
									let permission = resp.data.permission
									uni.setStorageSync("permission",permission)
									uni.switchTab({
										url:"../index/index"
									})
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">
	@import url("register.less");
</style>
